<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use think\Controller;
use think\Db;
use app\api\controller\common;
use app\index\controller\Base;


class Index extends Base
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    protected $task_model = null;

    protected $task_key = null;


    public function _initialize()
    {
        parent::_initialize();

        $this->task_key = new \app\common\model\shop\task\Key;

        $this->task_model  = new \app\admin\model\shop\Task;
    }



    //任务大厅
    public  function index(){
        $type = input("type")?:1;
        $page = 1;
        $page_size = 10;
        $_where["left_task_num_one"] = array(">=",1);

        $_join[] = ["sd_shop_task k","k.task_id = a.task_id","left"];
        $list  = Db::name("shop_task_key")
            ->alias("a")
            ->join($_join)
            ->where("a.left_task_num_one",">=",1)
            ->where("k.task_type",$type)
            ->where("k.task_status",2)
            ->field("a.key_word,k.goods_sno,a.take_key_id")
            ->page($page,$page_size)
            ->group("a.take_key_id")
            ->order("a.create_time DESC")
            ->select();

        $this->assign("type",$type);

        $this->assign("list",$list);
        return $this->fetch('/index');
    }

    //任务列表
    public function  taskList(){
        //halt(input('page'));
        $task_type = input('type');//task_type:1；精刷单，2：隔日单，3：流量单
        $page      = input('page')?:1;//页数
        $page_size = input('page_size')?:10;//页码数
        $_join[] = ["sd_shop_task k","k.task_id = a.task_id","left"];
        $data["count"]  = ceil((Db::name("shop_task_key")->alias("a")->join($_join)->where("k.task_type",$task_type)->where("a.left_task_num_one",">=",1)->count())/$page_size);
        $data["type"]   = $task_type;
        $data["curr"]   = $page;
        $data["list"]   = Db::name("shop_task_key")
                    ->alias("a")
                    ->join($_join)
                    ->where("a.left_task_num_one",">=",1)
                    ->where("k.task_type",$task_type)
                    ->field("a.key_word,k.goods_sno,a.take_key_id")
                    ->page($page,$page_size)
                    ->group("a.take_key_id")
                    ->order("a.create_time DESC")
                    ->select();
        renderJson("任务列表",1,$data);

    }


    //注意事项
    public function attention($task_key_id=NULL){

        $this->assign("task_key_id",$task_key_id);
        return $this->fetch("shop/attention");

    }








    //邀请注册接口

    /**
     * 邀请注册的接口
     *
     */
    public function  invitationAccept(){


        $uid = $this->uid;

        //halt($this->uid);
        $role = Db::name("user")->where("uid",$uid)->value("role");
        if($role=="grouper"){

            file_put_contents( "first_block.txt", "邀请parent_id1".$uid.PHP_EOL, FILE_APPEND);
            $data["title"]="邀请注册";//标签

            $data["desc"] = "邀请你一起征服世界";//描述

            $data["link"] = config("app.sys_domain").url("index/index")."?parent_id=".$this->uid;//分享链接

            $data["imgUrl"] = config("app.sys_domain")."/static/img/avatar.png";//分享图标

            renderJson("分享成功",1,$data);

        }elseif($role=="user"){

            renderJson("组长才可以邀请注册！",0);

        }


    }




}
